﻿using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace Jst.Infrastructure.Web
{
    public static class SwaggerConfig
    {
        public static void AddSwaggerConfiguration(this IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();

                // 为每个 API 版本生成 Swagger 文档
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    c.SwaggerDoc(description.GroupName, new OpenApiInfo
                    {
                        Title = $"阿金的版本管理平台 API 文档 - {description.ApiVersion}",
                        Version = description.ApiVersion.ToString(),
                        Description = "阿金的版本管理平台 API 接口文档",
                        Contact = new OpenApiContact
                        {
                            Name = "欢迎老板的查看",
                            Url = new System.Uri("https://gitee.com/JSTGitee/jst-sqlsuager-core8-api")
                        },
                    });
                }

                var xmlFile = "Jst.EntityFrameworkCore.xml";
                var xmlPath = Path.Combine(System.AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);

                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "请输入Bearer {token}",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        System.Array.Empty<string>()
                    }
                });

                // 配置 DocInclusionPredicate
                c.DocInclusionPredicate((docName, apiDesc) =>
                {
                    var model = apiDesc.ActionDescriptor?.GetApiVersionModel();
                    if (model == null) return false;

                    // docName 可能是 v1、v2
                    var major = docName.Substring(1);          // "1" 或 "2"
                    return model.DeclaredApiVersions.Any(v => v.MajorVersion.ToString() == major) ||
                           model.ImplementedApiVersions.Any(v => v.MajorVersion.ToString() == major);
                });
            });
        }
    }
}
